package com.abvnet.project.water.project.service;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.text.Convert;
import com.abvnet.project.water.project.domain.ProjectFlow;
import com.abvnet.project.water.project.domain.ProjectFlowSewage;
import com.abvnet.project.water.project.mapper.ProjectFlowMapper;
import com.abvnet.project.water.project.mapper.ProjectFlowSewageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目流程 服务层实现
 * 
 * @author duj
 * @date 2019-08-02
 */
@Service
public class ProjectFlowServiceImpl implements IProjectFlowService {
	@Autowired
	private ProjectFlowMapper projectFlowMapper;

	@Autowired
    private ProjectFlowSewageMapper projectFlowSewageMapper;

	/**
     * 查询项目流程信息
     * 
     * @param flowId 项目流程ID
     * @return 项目流程信息
     */
    @Override
	public ProjectFlow selectProjectFlowById(Long flowId) {
	    return projectFlowMapper.selectProjectFlowById(flowId);
	}

    @Override
    public ProjectFlow selectProjectFlowByProjectId(Long projectId) {
        return projectFlowMapper.selectProjectFlowByProjectId(projectId);
    }

    @Override
    public ProjectFlow selectProjectFlowPicByProjectId(Long projectId) {
        return projectFlowMapper.selectProjectFlowPicByProjectId(projectId);
    }

    /**
     * 查询项目流程列表
     * 
     * @param projectFlow 项目流程信息
     * @return 项目流程集合
     */
	@Override
	public List<ProjectFlow> selectProjectFlowList(ProjectFlow projectFlow) {
	    return projectFlowMapper.selectProjectFlowList(projectFlow);
	}
	
    /**
     * 新增项目流程
     * 
     * @param projectFlow 项目流程信息
     * @return 结果
     */
	@Override
	public int insertProjectFlow(ProjectFlow projectFlow) {
	    return projectFlowMapper.insertProjectFlow(projectFlow);
	}
	
	/**
     * 修改项目流程
     * 
     * @param projectFlow 项目流程信息
     * @return 结果
     */
	@Override
    @Transactional
	public int updateProjectFlow(ProjectFlow projectFlow) {
	    if (StringUtils.isNotEmpty(projectFlow.getFlowIds())) {
			projectFlowSewageMapper.deleteProjectFlowSewageById(projectFlow.getProjectId());
			projectFlowSewageMapper.batchProjectFlowSewages(Arrays.stream(Convert.toIntArray(projectFlow.getFlowIds())).map(f -> ProjectFlowSewage.builder().projectId(projectFlow.getProjectId()).flowId(f).build()).collect(Collectors.toList()));
		}
	    return projectFlowMapper.updateProjectFlow(projectFlow);
	}

	/**
     * 删除项目流程对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteProjectFlowByIds(String ids) {
		return projectFlowMapper.deleteProjectFlowByIds(Convert.toStrArray(ids));
	}
	
}